package product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import product.domain.Brand;
import product.mapper.CategoryBrandRelationMapper;
import product.service.BrandService;
import product.mapper.BrandMapper;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author panghu
 * @description 针对表【pms_brand(品牌)】的数据库操作Service实现
 * @createDate 2023-03-04 21:03:08
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    @Autowired
    private CategoryBrandRelationMapper categoryBrandRelationMapper;

    @Override
    public Page<Brand> queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<Brand> queryWrapper = getQueryWrapper(params);
        Page<Brand> page = new Page<>(Integer.parseInt((String) params.get("page")), Integer.parseInt((String) params.get("limit")));
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    @Transactional
    public boolean updateDetail(Brand brand) {
        boolean result = this.updateById(brand);
        if (!ObjectUtils.isArray(brand.getName())) {
            boolean updateBrandResult = categoryBrandRelationMapper.updateBrandNameByBrandId(brand.getBrandId(), brand.getName());
            return result && updateBrandResult;
        }
        return result;
    }


    private LambdaQueryWrapper<Brand> getQueryWrapper(Map<String, Object> params) {
        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(params.containsKey("key"), Brand::getBrandId, params.get("key"));
        return wrapper;
    }
}




